<template>
  <div class="articlesOuter">
    <!-- 面包屑导航 -->
    <div class="tagTop">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/layout/portal' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 标题 -->
    <h3 class="title">文章管理</h3>
    <!-- 内容区 -->
    <div class="articles">
      <div class="articles_top">
        <!-- 搜索文章 -->
        <div class="articles_search">
          <el-input v-model="keyWord" placeholder="请输入文章关键字" clearable />
          <el-button type="primary" plain @click="findArticle">搜索</el-button>
        </div>

        <!-- 添加文章 -->
        <div class="articles_addBtn">
          <el-button type="primary" round @click="increaseBtn">添加文章</el-button>
        </div>
      </div>
      <!-- 文章列表 -->
      <div class="articles_list">
        <el-table height="400" :data="tableData" stripe style="width: 100%">
          <el-table-column align="center" prop="title" fixed="left" label="文章标题" width="160" />
          <!-- <el-table-column align="center" prop="articleCover" label="封面" width="100" /> -->
          <el-table-column align="center" label="封面" width="100">
            <template #default="scope">
              <img style="height: 80px;" :src="scope.row.articleCover" alt="">
            </template>
          </el-table-column>
          <el-table-column align="center" prop="introduction" label="简介" width="230" />
          <el-table-column align="center" prop="category" label="分类" width="120" />
          <el-table-column align="center" prop="tag" label="标签" width="120" />
          <el-table-column align="center" prop="articleStatus" label="文章状态" width="120" />
          <el-table-column align="center" prop="createTime" label="创建时间" width="160" />
          <el-table-column align="center" prop="updateTime" label="修改时间" width="160" />
          <el-table-column align="center" label="操作" width="260" fixed="right">
            <template #default="scope">
              <el-button type="primary" round size="small" v-if="scope.row.articleStatus==='草稿'"
                @click="release(scope.row._id)">
                发布
              </el-button>
              <el-button type="warning" round size="small" @click="upArticle(scope.row)">修改</el-button>
              <el-button type="danger" round size="small" @click="delArticle(scope.row._id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ArrowRight, Delete, Edit } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import router from "../../router";
import { timeChange } from '../../hooks/index'
import {
  getArticleApi,
  searchArticleApi,
  delArticleApi,
  releaseArticleApi
} from '../../service/apiModule/articleApi'

onMounted(() => {
  getArticle()
})

// 文章搜索关键字
const keyWord = ref("");

// 文章列表数据
const tableData = ref([
  {
    articleContent: "",
    articleCover: "",
    articleStatus: "",
    category: "",
    commentLikeFavorites: "",
    introduction: "",
    tag: "",
    title: "",
    _id: "",
    createTime: 0,
    updateTime: 0
  }
]);

// 查询文章
const getArticle = async () => {
  const { data: res } = await getArticleApi({ id: '' })
  res.data.list.forEach((item: any) => {
    item.createTime = timeChange(item.createTime)
    item.updateTime = timeChange(item.updateTime)
  })
  tableData.value = res.data.list
}

// 添加文章
const increaseBtn = () => {
  router.push("/layout/increaseArticle");
};

// 修改文章
const upArticle = (data: any) => {
  router.push({
    path: "/layout/increaseArticle",
    query: {
      id: data._id
    }
  });
}

// 搜索文章
const findArticle = async () => {
  const { data: res } = await searchArticleApi({ keyWord: keyWord.value })
  if (res.status.code === 200) {
    tableData.value = res.data
    res.data.forEach((item: any) => {
      item.createTime = timeChange(item.createTime)
      item.updateTime = timeChange(item.updateTime)
    })
  } else {
    ElMessage.error('搜索失败或数据为空!')
  }
}

// 删除文章
const delArticle = async (id: String) => {
  const { data: res } = await delArticleApi({ id })
  if (res.status.code === 200) {
    ElMessage.success('删除成功!')
    getArticle()
  } else {
    ElMessage.error('删除失败!')
    console.log(res)
  }
}

// 发布文章
const release = async (id: String) => {
  const { data: res } = await releaseArticleApi({ id })

  if (res.status.code !== 200) {
    ElMessage.error('文章发布失败!')
  } else {
    ElMessage.success('文章发布成功!')
    getArticle()
  }
}

// 分页数据
const currentPage4 = ref(1);
const pageSize4 = ref(10);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
let total = ref(20);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
};
</script>

<style lang="scss" scoped>
$H: 91vh;

.articlesOuter {
  width: 100%;
  height: $H;
  background-color: rgb(243, 243, 243);
  padding: 20px;

  .tagTop {
    width: 100%;
    background-color: rgba(255, 255, 255, 0.6);
    padding: 10px 10px;
    border-radius: 10px;
  }

  .title {
    margin-top: 20px;
    margin-bottom: 20px;
  }

  .articles {
    background-color: #fff;
    padding: 10px 0 0 10px;
    border-radius: 10px;
    min-height: 60%;
    max-height: 76%;

    .articles_top {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 30px 10px 10px;

      .articles_search {
        width: 26%;
        display: flex;
        align-items: center;

        &>.el-input {
          margin-right: 6px;
        }
      }
    }
  }

  .demo-pagination-block {
    height: 52px;
    background-color: #fff;
    margin-top: 10px;
    padding: 10px;
    border-radius: 10px;
    position: relative;

    .el-pagination {
      position: absolute;
      right: 10px;
    }
  }
}
</style>
